package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.NoteFavoriteMapper;
import com.ruoyi.system.domain.NoteFavorite;
import com.ruoyi.system.service.INoteFavoriteService;

/**
 * 笔记收藏记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class NoteFavoriteServiceImpl implements INoteFavoriteService 
{
    @Autowired
    private NoteFavoriteMapper noteFavoriteMapper;

    /**
     * 查询笔记收藏记录
     * 
     * @param favoriteId 笔记收藏记录主键
     * @return 笔记收藏记录
     */
    @Override
    public NoteFavorite selectNoteFavoriteByFavoriteId(Long favoriteId)
    {
        return noteFavoriteMapper.selectNoteFavoriteByFavoriteId(favoriteId);
    }

    /**
     * 查询笔记收藏记录列表
     * 
     * @param noteFavorite 笔记收藏记录
     * @return 笔记收藏记录
     */
    @Override
    public List<NoteFavorite> selectNoteFavoriteList(NoteFavorite noteFavorite)
    {
        return noteFavoriteMapper.selectNoteFavoriteList(noteFavorite);
    }

    /**
     * 新增笔记收藏记录
     * 
     * @param noteFavorite 笔记收藏记录
     * @return 结果
     */
    @Override
    public int insertNoteFavorite(NoteFavorite noteFavorite)
    {
        noteFavorite.setCreateTime(DateUtils.getNowDate());
        return noteFavoriteMapper.insertNoteFavorite(noteFavorite);
    }

    /**
     * 修改笔记收藏记录
     * 
     * @param noteFavorite 笔记收藏记录
     * @return 结果
     */
    @Override
    public int updateNoteFavorite(NoteFavorite noteFavorite)
    {
        noteFavorite.setUpdateTime(DateUtils.getNowDate());
        return noteFavoriteMapper.updateNoteFavorite(noteFavorite);
    }

    /**
     * 批量删除笔记收藏记录
     * 
     * @param favoriteIds 需要删除的笔记收藏记录主键
     * @return 结果
     */
    @Override
    public int deleteNoteFavoriteByFavoriteIds(Long[] favoriteIds)
    {
        return noteFavoriteMapper.deleteNoteFavoriteByFavoriteIds(favoriteIds);
    }

    /**
     * 删除笔记收藏记录信息
     * 
     * @param favoriteId 笔记收藏记录主键
     * @return 结果
     */
    @Override
    public int deleteNoteFavoriteByFavoriteId(Long favoriteId)
    {
        return noteFavoriteMapper.deleteNoteFavoriteByFavoriteId(favoriteId);
    }

    /**
     * 切换收藏状态
     * 
     * @param userId 用户ID
     * @param noteType 笔记类型
     * @param noteId 笔记ID
     * @param folderName 收藏夹名称
     * @return 结果（true=已收藏，false=已取消收藏）
     */
    @Override
    public boolean toggleFavorite(Long userId, String noteType, Long noteId, String folderName)
    {
        // 检查是否已收藏
        NoteFavorite existingFavorite = noteFavoriteMapper.selectNoteFavoriteByUserAndNote(userId, noteType, noteId);
        boolean isFavorited = existingFavorite != null;
        
        if (isFavorited) {
            // 已收藏，取消收藏
            noteFavoriteMapper.deleteNoteFavoriteByUserAndNote(userId, noteType, noteId);
            return false;
        } else {
            // 未收藏，添加收藏
            NoteFavorite noteFavorite = new NoteFavorite();
            noteFavorite.setUserId(userId);
            noteFavorite.setNoteType(noteType);
            noteFavorite.setNoteId(noteId);
            noteFavorite.setFolderName(folderName != null ? folderName : "默认收藏夹");
            noteFavorite.setCreateTime(DateUtils.getNowDate());
            noteFavoriteMapper.insertNoteFavorite(noteFavorite);
            return true;
        }
    }

    /**
     * 检查用户是否已收藏
     * 
     * @param userId 用户ID
     * @param noteType 笔记类型
     * @param noteId 笔记ID
     * @return 结果
     */
    @Override
    public boolean isFavorited(Long userId, String noteType, Long noteId)
    {
        NoteFavorite favorite = noteFavoriteMapper.selectNoteFavoriteByUserAndNote(userId, noteType, noteId);
        return favorite != null;
    }

    /**
     * 统计笔记收藏数
     * 
     * @param noteType 笔记类型
     * @param noteId 笔记ID
     * @return 收藏数
     */
    @Override
    public int countFavoritesByNote(String noteType, Long noteId)
    {
        return noteFavoriteMapper.countFavoritesByNote(noteType, noteId);
    }

    /**
     * 查询用户收藏的笔记列表
     * 
     * @param userId 用户ID
     * @return 收藏记录集合
     */
    @Override
    public List<NoteFavorite> selectUserFavoriteNotes(Long userId)
    {
        return noteFavoriteMapper.selectUserFavoriteNotes(userId);
    }

    /**
     * 查询用户的收藏夹列表
     * 
     * @param userId 用户ID
     * @return 收藏夹名称列表
     */
    @Override
    public List<String> selectUserFolders(Long userId)
    {
        return noteFavoriteMapper.selectUserFolders(userId);
    }

    /**
     * 根据收藏夹查询收藏记录
     * 
     * @param userId 用户ID
     * @param folderName 收藏夹名称
     * @return 收藏记录集合
     */
    @Override
    public List<NoteFavorite> selectFavoritesByFolder(Long userId, String folderName)
    {
        return noteFavoriteMapper.selectFavoritesByFolder(userId, folderName);
    }
}